//
//  WMH_WSBS_ViewController.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/22.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  网上办事 分类，分组

#import "WMH_WSBS_ViewController.h"
#import "AutoButtonTableView.h"
#import "WSBSCateListModel.h"
#import "WHM_WSBS_ItemList_ViewController.h"

#define TaskId_GetWsbsGroup 1       //网上办事分组
#define TaskId_GetWsbsCategory 2    //网上办事分类

@interface WMH_WSBS_ViewController ()<AutoButtonTableViewDelegate>
{
    
    NSMutableArray *wsbsGroup;
    
    NSMutableArray *SMBSArray;
    NSMutableArray *WSBSItemNameArray;
    
    int segIndex;
}

@property(retain,nonatomic) UISegmentedControl *seg;
@property(retain,nonatomic) AutoButtonTableView *autoButtonTable;

@end

@implementation WMH_WSBS_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    wsbsGroup       = [[NSMutableArray alloc]init];
    
    SMBSArray       = [[NSMutableArray alloc]init];
    WSBSItemNameArray   = [[NSMutableArray alloc]init];
    
    [self getWsbsGroup];
}

-(void)segValueChange:(UISegmentedControl *)segment{
    segIndex = segment.selectedSegmentIndex;
    WSBSCateListModel *info = [wsbsGroup objectAtIndex:segIndex];
    [self getWsbsCategory:info.CateGuid taskid:TaskId_GetWsbsCategory];
}


//获取网上办事分组
-(void)getWsbsGroup{
    [self showMBPHUD];
    [self startTask:@"Task_GetWsbsGroup" withParams:nil withTaskId:TaskId_GetWsbsGroup];
}

//获取网上办事分类
-(void)getWsbsCategory:(NSString *)parentcategory taskid:(int)_taskid{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:parentcategory forKey:@"parentcategory"];
    [self startTask:@"Task_GetWsbsCategory" withParams:params withTaskId:_taskid];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    
    [self hideMBPHUD];
    
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    
    if(taskId == TaskId_GetWsbsGroup){
        wsbsGroup = [arr objectAtIndex:2];
        if([wsbsGroup count]>0){
            
            NSMutableString *itemstr = [[NSMutableString alloc]init];
            for (int i=0; i<[wsbsGroup count]; i++) {
                WSBSCateListModel *info = [wsbsGroup objectAtIndex:i];
                if (i==[wsbsGroup count]-1) {
                    [itemstr appendString:[NSString stringWithFormat:@"%@",info.CateName]];
                }else{
                    [itemstr appendString:[NSString stringWithFormat:@"%@;",info.CateName]];
                }
            }
            NSArray *items = [itemstr componentsSeparatedByString:@";"];
            
            _seg = [[UISegmentedControl alloc]initWithItems:items];
            _seg.frame = CGRectMake(10, 10, AppWidth-20, 30);
            [_seg addTarget:self action:@selector(segValueChange:) forControlEvents:UIControlEventValueChanged];
            [self.view addSubview:_seg];
            
            [_seg setSelectedSegmentIndex:0];
            
            segIndex = 0;
            
            //获取第一分组
            WSBSCateListModel *info = [wsbsGroup objectAtIndex:0];
            [self getWsbsCategory:info.CateGuid taskid:TaskId_GetWsbsCategory];
            
        }
    }
    //办事分类
    if(taskId == TaskId_GetWsbsCategory){
        NSMutableArray *dataArray = [arr objectAtIndex:2];
        if ([dataArray count]>0) {
            SMBSArray = dataArray;
            [self addAutoTable:dataArray];
        }
    }
}

//动态生成栏目分类
-(void)addAutoTable:(NSMutableArray *)data{
    
    [_autoButtonTable removeFromSuperview];
    
    _autoButtonTable = [[AutoButtonTableView alloc]initWithFrame:CGRectMake(0, _seg.bottom, AppWidth, AppHeight-SystemTakeHeight-30)];
    _autoButtonTable.tag = 2;
    _autoButtonTable.backgroundColor = [UIColor clearColor];
    _autoButtonTable.titleFontSize = 12;
    _autoButtonTable.delegate = self;
    
    [WSBSItemNameArray removeAllObjects];
    for (int i=0; i<[data count]; i++) {
        WSBSCateListModel *m = [data objectAtIndex:i];
        [WSBSItemNameArray addObject:m.CateName];
    }
    _autoButtonTable.buttonTextArray = [[NSArray alloc] initWithArray:WSBSItemNameArray];
    [self.view addSubview:_autoButtonTable];
    [_autoButtonTable show];
}

#pragma mark 栏目分类 点击事件
- (void)buttonPressed:(NSString *)buttonTex buttonTag:(int)tag {
    NSLog(@"click button %@",[WSBSItemNameArray objectAtIndex:tag]);
    WSBSCateListModel *info = [SMBSArray objectAtIndex:tag];
    WHM_WSBS_ItemList_ViewController *view = [[WHM_WSBS_ItemList_ViewController alloc]init];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:info.CateGuid forKey:@"categuid"];
    [params setObject:info.CateName forKey:@"title"];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
    
}
@end

